package com.ly.hotel.spainterface.nm;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.ly.hotel.R;
import com.ly.hotel.spainterface.nm.bean.NmXFItemBean;
import com.ly.hotel.spainterface.nm.event.XFUpdateItemHideEvent;
import com.ly.hotel.spainterface.nm.event.XFUpdateItemSortEvent;

import java.util.List;

import de.greenrobot.event.EventBus;

public class NmXFOperItemAdapter extends BaseAdapter {
	NmXiaoFeiActivity mContext;
	LayoutInflater mInflater;
	private List<NmXFItemBean> xtItemList;

	public NmXFOperItemAdapter(NmXiaoFeiActivity c, List<NmXFItemBean> xtItemList) {
		this.xtItemList = xtItemList;
		mContext = c;
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return xtItemList.size();
	}

	@Override
	public Object getItem(int postion) {
		return xtItemList.get(postion);
	}

	@Override
	public long getItemId(int postion) {
		return postion;
	}
	

	@Override
	public View getView(int postion, View contentView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final int pos = postion;
		ViewHolder holder;
		View view;
		if (contentView == null) {
			holder = new ViewHolder();
			view = mInflater.inflate(R.layout.xf_oper_type, null);
			holder.mbtn = (TextView) view.findViewById(R.id.xf_oper_name);
			holder.sbtn = (Switch) view.findViewById(R.id.xf_oper_switch);
			holder.mimg = (ImageView) view.findViewById(R.id.xf_oper_top);
			view.setTag(holder);
		} else {
			view = contentView;
			holder = (ViewHolder) view.getTag();
		}
		holder.sbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				NmXFItemBean ib = xtItemList.get(pos);
				ib.setShow(isChecked);
				String id = ib.getId();
				EventBus.getDefault().post(new XFUpdateItemHideEvent(id,isChecked));
			}
		});

        holder.mimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(pos !=0) {
                    NmXFItemBean tb = xtItemList.get(pos);
                    EventBus.getDefault().post(new XFUpdateItemSortEvent(tb));
                }else{
                    Toast.makeText(mContext,"已经置顶，无效点击",Toast.LENGTH_SHORT).show();
                }
            }
        });
		holder.mbtn.setText(xtItemList.get(postion).getName());
		if(xtItemList.get(postion).getShow()){
			holder.sbtn.setChecked(true);
			holder.mimg.setVisibility(View.VISIBLE);
		}else{
			holder.sbtn.setChecked(false);
			holder.mimg.setVisibility(View.INVISIBLE);
		}
		return view;
	}

	static class ViewHolder {
		TextView mbtn;
		Switch sbtn;
		ImageView mimg;
	}
}
